# -*- coding: utf-8 -*- 
from django.db import models
from intraorg.members.models import *
from django.core.exceptions import ObjectDoesNotExist

class Project(models.Model):
    '''This model handles information about projects run by our organisation
    '''
    name = models.CharField(maxlength=20,unique=True)
    owner = models.ForeignKey(Member,verbose_name = u'opiekun projektu')
    logo = models.ImageField(upload_to="logo")
    start_date = models.DateTimeField(u'data rozpoczęcia projektu',auto_now_add=True)
    end_date = models.DateTimeField(u'data zakończenia projektu',blank=True,null=True)
    est_date = models.DateTimeField(u'przewidywana data ukończenia projektu',blank=True,null=True)
    status = models.ForeignKey("ProjectStatus",verbose_name = u'stan projektu')
    webpage = models.CharField(maxlength=40,blank=True)
    docspage = models.CharField(maxlength=40,blank=True)
    about = models.TextField(blank=True)
    class Meta:
        verbose_name = u'project'
        verbose_name_plural = u'projekty'
    class Admin:
        list_display = ("name","owner","status")
        search_fields = ['name',"about"]
    def save(self):
      super(Project,self).save()
      try:
          ProjectTeam.objects.get(member=self.owner,project=self.id)
      except ObjectDoesNotExist:
          ProjectTeam(project=self,member=self.owner).save()
    def __unicode__(self):
        return u'%s' % self.name
    def get_absolute_url(self):
        return "/projects/%i/" % self.id
        
        
class ProjectTeam(models.Model):
    '''This class is responsible for sticking together information about projects and people runnig them.
    '''
    project = models.ForeignKey(Project)
    member = models.ForeignKey(Member)
    responsibilities = models.TextField()
    join_date = models.DateTimeField(u'data przyłączenia do projektu',auto_now_add=True)
    left_date = models.DateTimeField(u'data opuszczenia projektu',blank=True,null=True)
    def __unicode__(self):
        return u''.join((self.project.name,': ',self.member.__unicode__()))
    class Admin:
        pass
    class Meta:
        verbose_name = u'członek projektu'
        verbose_name_plural = u'członkowie projektu'
    def get_absolute_url(self):
        return "/projects/teams/%i/" % self.id
    
class ProjectStatus(models.Model):
    '''Each project should have a status field, we need to be flexible so here we are'''
    name = models.CharField(maxlength=40)
    def __unicode__(self):
        return u'%s' % self.name
    class Admin:
        pass
    class Meta:
        verbose_name = u'stan projektu'
        verbose_name_plural = u'stany projektów'

class ProjectIssueType(models.Model):
    '''Each project issue should have a type field, we need to be flexible so here we are'''
    name = models.CharField(maxlength=40)
    def __unicode__(self):
        return u'%s' % self.name
    class Admin:
        pass
    class Meta:
        verbose_name = u'typ issues'
        verbose_name_plural = u'typy issues'

class ProjectIssue(models.Model):
    '''Each project should be able to add issues'''
    name = models.CharField(maxlength=40,core=True)
    text = models.TextField()
    type = models.ForeignKey(ProjectIssueType)
    starred = models.BooleanField(default=False)
    project = models.ForeignKey(Project,edit_inline=models.TABULAR, num_in_admin=1)
    def __unicode__(self):
        return u'%s' % self.name
    class Admin:
        pass
    class Meta:
        verbose_name = u'issue projektu'
        verbose_name_plural = u'issues projektów'
        
class JobRequest(models.Model):
    '''Users can look for job'''
    name = models.CharField(maxlength=40,core=True)
    text = models.TextField()
    user = models.ForeignKey(Member)
    def __unicode__(self):
        return u'%s' % self.name
    class Admin:
        pass
    class Meta:
        verbose_name = u'Praca poszukiwana'
        verbose_name_plural = u'Praca poszukiwana'
    def get_absolute_url(self):
        return "/projects/jobrequests/%i/" % self.id
    
class JobOffert(models.Model):
    '''Each project manager can search for people to work(just between us it should be called joboffer)'''
    name = models.CharField(maxlength=40,core=True)
    description = models.TextField()
    responsibilities = models.TextField()
    requirements = models.TextField()
    project = models.ForeignKey(Project,edit_inline=models.TABULAR, num_in_admin=1)
    def __unicode__(self):
        return u'%s' % self.name
    class Admin:
        pass
    class Meta:
        verbose_name = u'oferta pracy'
        verbose_name_plural = u'oferty pracy'
    def get_absolute_url(self):
        return "/projects/jobofferts/%i/" % self.id
